package com.github_dididipapa.mall.product.service.impl;

import com.github_dididipapa.mall.product.dao.AttrDao;
import com.github_dididipapa.mall.product.service.AttrService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github_dididipapa.common.utils.PageUtils;
import com.github_dididipapa.common.utils.Query;

import com.github_dididipapa.mall.product.dao.AttrAttrgroupRelationDao;
import com.github_dididipapa.mall.product.entity.AttrAttrgroupRelationEntity;
import com.github_dididipapa.mall.product.service.AttrAttrgroupRelationService;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@RequiredArgsConstructor
@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    private final AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }


    @Transactional
    @Override
    public void saveDetail(AttrAttrgroupRelationEntity relationEntity) {
        this.save(relationEntity);

    }

    @Transactional
    @Override
    //仅删除联系，没有冗余字段
    public void deleteDetail(AttrAttrgroupRelationEntity relationEntity) {
        attrAttrgroupRelationDao.delRelation(relationEntity.getAttrId(), relationEntity.getAttrGroupId());
    }


    @Override
    public long getAttrGroupIdByAttrId(Long attrId) {
        AttrAttrgroupRelationEntity relationEntity = attrAttrgroupRelationDao
                .selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>()
                        .eq("attr_id", attrId));

        if(relationEntity == null) {
            log.error("attrid: {}  对应的groupId不存在",attrId);
            return 0;
        }

        return relationEntity.getAttrGroupId();
    }


}